//
//  DDSpecCountHeaderView.m
//  Dingdong
//
//  Created by 程强 on 16/12/12.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDSpecCountHeaderView.h"

@implementation DDSpecCountHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        KKLabel *titleLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"Count",RGBColor(150, 150, 150),[UIFont systemFontOfSize:14.0],NSTextAlignmentLeft);
        }];
        [self addSubview:titleLbl];
        [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.width.equalTo(@60);
            make.height.equalTo(self);
            make.centerY.equalTo(self);
        }];
        
        self.countBar = [[DDCountBar alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        [self addSubview:_countBar];
        [_countBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(titleLbl);
            make.right.equalTo(self).offset(-20);
            make.width.equalTo(@120);
            make.height.equalTo(@30);
        }];
        
        weak_Self(self);
        [_countBar countBarKeyboardShow:^(CGFloat height){
            CGFloat keyBoard_y = ScreenHeight-height;
            CGRect barFrame = [self convertRect:_countBar.frame toView:[UIApplication sharedApplication].keyWindow];
            KKLog(@"数量框的位置%@",NSStringFromCGRect(barFrame));
            if (keyBoard_y<barFrame.origin.y+30) {
                CGFloat distance = fabs(keyBoard_y-(barFrame.origin.y+30));
                if (weakSelf.showBlock) {
                    weakSelf.showBlock(distance);
                }
            }
        } hiddenKeyboard:^(CGFloat height){
            if (weakSelf.displayBlock) {
                weakSelf.displayBlock(height);
            }
        }];

    }
    return self;
}
- (void)headerCountBarShowKeyboard:(HeaderKeyboardCallBack)show display:(HeaderKeyboardCallBack)display
{
    self.showBlock = show;
    self.displayBlock = display;
}
- (NSString *)inputCount
{
    return self.countBar.countTxf.text;
}
@end
